package com.gzx.qc.modular.voice.text.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.qc.core.config.QaKeywordInitializationConfig;
import com.gzx.qc.modular.voice.check.entity.GzxVoiceCheckDetail;
import com.gzx.qc.modular.voice.check.service.GzxVoiceCheckService;
import com.gzx.qc.modular.voice.text.entity.GzxHtVoiceText;
import com.gzx.qc.modular.voice.text.mapper.GzxHtVoiceTextMapper;
import com.gzx.qc.modular.voice.text.service.GzxHtVoiceTextService;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zjt
 * @description
 * @since 2025/1/22 09:09
 */
@Service
@RequiredArgsConstructor
public class GzxHtVoiceTextServiceImpl implements GzxHtVoiceTextService {

    private final GzxHtVoiceTextMapper htVoiceTextMapper;

    private final GzxVoiceCheckService voiceCheckService;

    private final QaKeywordInitializationConfig qaKeywordInitializationConfig;

    /**
     * 接收语音转文字的对象
     *
     * @param consumerRecordList
     */
    @Override
    @DS("qc")
    public void saveVoiceText(List<ConsumerRecord<String, String>> consumerRecordList) {
        List<GzxHtVoiceText> htVoiceTextList = new ArrayList<>();
//        List<GzxVoiceCheckDetail> voiceCheckDetailList = new ArrayList<>();
        for (ConsumerRecord<String, String> consumerRecord : consumerRecordList) {
            GzxHtVoiceText htVoiceText = JSONObject.parseObject(consumerRecord.value(), GzxHtVoiceText.class);
            htVoiceTextList.add(htVoiceText);
//            Set<String> contentKeySet = HanLP.segment(htVoiceText.getContent()).stream()
//                    .map(term -> term.word)
//                    .collect(Collectors.toCollection(HashSet::new));
//            Set<String> keywordSet = qaKeywordInitializationConfig.verifyKeyWord(htVoiceText.getSpeakerRole(), contentKeySet);
//            if (CollectionUtil.isNotEmpty(keywordSet)) {
//                GzxVoiceCheckDetail voiceCheck = new GzxVoiceCheckDetail();
//                voiceCheck.setCallId(htVoiceText.getCallId());
//                voiceCheck.setKeyword(String.join(",", keywordSet));
//                voiceCheck.setContent(htVoiceText.getContent());
//                voiceCheckDetailList.add(voiceCheck);
//            } else {
//                GzxVoiceCheckDetail voiceCheck = new GzxVoiceCheckDetail();
//                voiceCheck.setCallId(htVoiceText.getCallId());
//                voiceCheck.setKeyword("");
//                voiceCheck.setContent(htVoiceText.getContent());
//                voiceCheckDetailList.add(voiceCheck);
//            }
        }
        htVoiceTextMapper.insertBatch(htVoiceTextList);
//        if (CollectionUtil.isNotEmpty(voiceCheckDetailList)) {
//            voiceCheckService.insertBatchDetail(voiceCheckDetailList);
//            String currentTime = CommonDateUtil.getCurrentTimeStr();
//            List<GzxVoiceCheck> voiceCheckList = voiceCheckDetailList.stream()
//                    .map(a -> new GzxVoiceCheck(a.getCallId(), currentTime)).collect(Collectors.toMap(
//                            GzxVoiceCheck::getCallId,
//                            v -> v, (existing, replacement) -> existing)).values().stream().toList();
//            voiceCheckService.insertIgnore(voiceCheckList);
//        }
    }

    /**
     * 根据CallID获取通话文本
     */
    @Override
    @DS("qc")
    public List<GzxHtVoiceText> getTextByCallId(String callId) {
        List<GzxHtVoiceText> gzxHtVoiceTexts = htVoiceTextMapper.selectList(Wrappers.<GzxHtVoiceText>lambdaQuery()
                .eq(GzxHtVoiceText::getCallId, callId)
                .orderByAsc(GzxHtVoiceText::getChatTime));
        LambdaQueryWrapper<GzxVoiceCheckDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GzxVoiceCheckDetail::getCallId, callId);
        List<GzxVoiceCheckDetail> gzxVoiceCheckDetails = voiceCheckService.getGzxVoiceCheckDetailBywarpper(wrapper);
        Map<String, GzxHtVoiceText> textAndGzxHtVoiceTextHashMap = gzxHtVoiceTexts.stream()
                .collect(Collectors.toMap(GzxHtVoiceText::getContent, v -> v, (existing, replacement) -> existing));
        for (GzxVoiceCheckDetail gzxVoiceCheckDetail : gzxVoiceCheckDetails) {
            if (textAndGzxHtVoiceTextHashMap.containsKey(gzxVoiceCheckDetail.getContent())) {
                String keyword = gzxVoiceCheckDetail.getKeyword();
                String replace = textAndGzxHtVoiceTextHashMap.get(gzxVoiceCheckDetail.getContent()).getContent();
                for (String s : keyword.split(",")) {
                    String replacement = "<span class=\"highlight\">" + s + "</span>";
                    replace = replace.replace(s, replacement);
                }
                textAndGzxHtVoiceTextHashMap.get(gzxVoiceCheckDetail.getContent()).setContent(replace);
            }
        }
        return gzxHtVoiceTexts;
    }
}
